﻿/* 
 * Copyright 2010 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Diagnostics;

namespace Ngilead.Common
{
    /// <summary>
    /// Id / Class structure for proxy information collection handling
    /// </summary>
    //[DataContract]
    [DataContract]
    public class SerializableId
    {
        #region Private

        private int? _hashCode;
        private object _id;
        private string _entityName;

        #endregion

        #region Public

        /// <summary>
        /// Hash code for non persistent and transient values
        /// </summary>
        [DataMember]
        public int? HashCode
        {
            get { return _hashCode; }
            set { _hashCode = value; }
        }

        /// <summary>
        /// Id
        /// </summary>
        [DataMember]
        public object Id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// Entity name
        /// </summary>
        [DataMember]
        public string EntityName
        {
            get { return _entityName; }
            set { _entityName = value; }
        }

        #endregion

        #region Override

        /// <summary>
        /// Override GetHashCode()
        /// </summary>
        /// <returns>HashCode</returns>
        public override int GetHashCode()
        {
            if (_hashCode.HasValue)
            {
                return _hashCode.Value;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// Override Equals()
        /// </summary>
        /// <param name="obj">Object</param>
        /// <returns>true/false</returns>
        public override bool Equals(object obj)
        {
            SerializableId temp = obj as SerializableId;
            if (temp == null)
            {
                return false;
            }

            // Check entity name
            if (EntityName == null)
            {
                return (temp.EntityName == null);
            }
            else if (EntityName.Equals(temp.EntityName) == false)
            {
                return false;
            }

            // Check id or hashcode
            if (Id == null)
            {
                if (_hashCode != null)
                {
                    return _hashCode.Equals(temp._hashCode);
                }
                else
                {
                    return temp._hashCode == null;
                }
            }
            else
            {
                return Id.Equals(temp.Id);
            }
        }

        /// <summary>
        /// Override ToString()
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return String.Format("Id : {0} - HashCode : {1} - EntityName : {2}", Id, HashCode, EntityName);
        }

        #endregion
    }
}
